 Ext.ns("Pengtu.Ext");

/*
 * Ext JS Library 2.0.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing extjs.com
 * 
 * extjs.com/license
 */
 Pengtu.Ext.ContractPanel= function(config){
    Ext.apply(this, config);
    Pengtu.Ext.ContractPanel.superclass.constructor.call(this);
};

Ext.extend(Pengtu.Ext.ContractPanel ,Ext.Panel,{
	closable: true,   //关闭
  	autoScroll:true,  //自动显示工具条
  	layout:"fit",     //
  	fid: null,
    deleteURL: null,
    saveURL: null,
    loadURL:null,
    treeGrid: null,
  	initWin:function(width,title)
    {
		if(!this.fp){
			 this.fp=this.createForm();
		}
		this.onAddPanel();
		this.fp.setTitle(null);
		var width = this.width+10;
		var title = this.title+"新增";
    	var win=new Ext.Window({
			width:width,
			buttonAlign:"center",
			title:title,
			modal:true,
			shadow:true,
			closeAction:"hide",
			items:[this.fp]				  
		});
		return win;
    },
    createWin:function()
	{	
		if(!this.win){
		this.win=this.initWin();
		this.win.on("close",function(){this.win=null;this.fp=null;},this);
		}
		this.win.show();
	},
	showWin:function()
	{
		this.createWin();
		this.onReset();
	},
	addCloseButton: function () {
	    var fp = this.fp;
	    fp.addButton('关闭',this.closeWin,this);
	},
	closeWin:function()
	{
		if(this.win)this.win.close();
		this.win=null;
		this.fp = null;
		this.reloadData();
	},
	onAddPanel:function()
	{
		this.addResetButton();
		this.addSaveButton();
		this.addCloseButton();
		return this.fp;
	},
	onEditPanel:function()
	{
		if(!this.fp){
			this.fp=this.createForm();
		}
		this.addLoadButton();
		this.addSaveButton();
		this.addDeleteButton();
		this.loadData();
		return this.fp;
	},
	addResetButton: function () {
	    var fp = this.fp;
	    fp.addButton('重置',this.onReset,this);
	},
	onReset:function()
	{
	 this.fp.form.reset();
	},
	addSaveButton: function () {
	    var fp = this.fp;
	    fp.addButton('保存',this.onSave,this);
	},
	onSave:function(){
		var id=this.fp.getForm().findField("id").getValue();
		var fid =this.getFid();
		var fparam =this.fparam;
		var url = this.saveURL;
		if(fid){
			url = url+"?"+fparam+"="+fid;
		}
		var object = this;
		this.fp.form.submit({
				scope:object,
				waitMsg:'正在保存。。。',
	            url:url,
	            method:'POST',
	            success:function(){
	            	 Ext.example.msg('信息', '数据保存成功！');  
	         		if(!fid){
	         			this.closeWin();
	        		}   	
	            },
	            scope:this
		});	
	},
	addLoadButton: function () {
	    var fp = this.fp;
	    fp.addButton('刷新',this.loadData,this);
	},
	loadData:function(){
    	var id = this.getId();
		var url  = this.loadURL+id;
		this.fp.getForm().load({url:url});
	},
  	/**
	刷新列表纪录
	*/
	reloadData :function (){
   		this.treeGrid.root.reload();
	},
	addDeleteButton: function(){
	    var fp = this.fp;
	    fp.addButton('删除',this.onDelete,this);
	},
	/**
	 * 删除一条记录
	 */
	 onDelete:function (){
		var id=this.fp.form.findField("id").getValue();	
		var url = this.deleteURL+this.getId();
		var objcet =this;
   		if(id ==''||id==null){
 	 	Ext.Msg.show({title: '没有选择项',msg: '请至少选择一项',modal: true,
                	 icon: Ext.Msg.INFO,buttons: Ext.Msg.OK}); return ;}
    	Ext.Msg.confirm('删除操作', '删除将无法恢复,是否继续?', function(btn, text){
 		if (btn == 'yes'){              	 
	   	// 数据库操作
 			
	   	Ext.Ajax.request({
	    scope:objcet,
        url: url,
        success: function(response, options){
        	 try {
					var responseData = Ext.decode(response.responseText);
           			if (responseData.success == true) {
           				this.reloadData();
           	    	 Ext.example.msg('信息', '数据删除成功！');  
            		 } else { Ext.example.msg('提示', '删除失败！');}
            } catch (e) { Ext.example.msg('提示', '删除失败！请稍后再尝试');}
        },
        failure: function(response, options){Ext.example.msg('警告!', '服务端发生错误,数据删除失败!');}
    }); 
  	}
	}); 			 
 	},
 	getFid : function () {
 		return this.fid;
 	},
 	initComponent : function(){  
 		Pengtu.Ext.ContractPanel.superclass.initComponent.call(this);
 		if(!this.width){
 			this.width = 350;
 		};
 		if(!this.title){
 			this.title = "分部分项";
 		};
	}
});